package com.kk.license.client.interceptor;

import com.kk.license.common.LicenseVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TODO
 * @Author wangkang
 * @Create 2023-07-19 17:02
 * @Version 1.0
 */
@Component
@Slf4j
@ConditionalOnClass(name={"com.kk.license.starter.config.LicenseAutoConfiguration"})
public class ChannelNumbersInterceptor  implements HandlerInterceptor{

    /**
     * 进入controller层之前拦截请求
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入拦截器，验证设备通道数量");
        LicenseVerify channelVerify = new LicenseVerify();

        //校验通道
        Integer verify = channelVerify.getChannels();

        if (verify > 0) {
            log.info("验证通过，通道可用:{}",verify);
            return true;
        } else {
            log.info("验证失败，证书无效");

            return false;
        }
    }

}